package com.example.dmdomain;

import com.example.dmdomain.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User>  {

    @Query(value = "select * from users where phone=:phone", nativeQuery = true)
    User findUsersByPhone(@Param("phone") String phone);

    @Query(value = "select * from users where open_id=:openId", nativeQuery = true)
    User findUsersByOpenId(@Param("openId") String openId);

    @Query(value = "UPDATE users set open_id=:openId WHERE phone=:phone ", nativeQuery = true)
    @Transactional
    @Modifying
    void updateLogOpenId(@Param("phone") String phone, @Param("openId") String openId);

    @Query(value = "delete from users WHERE phone=:phone and open_id = null", nativeQuery = true)
    void deletePhone(@Param("phone") String phone);
    @Query(value = "select * from users where user_id=:userId", nativeQuery = true)
    User findUsersByUserID(@Param("userId") String userId);

    @Query(value = "select * from users where open_id=:openId", nativeQuery = true)
    User findUsersByOpenID(@Param("openId") String openId);
}
